<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户验证</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 50px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 100px;
        }
        input {
            padding: 5px;
            width: 200px;
        }
        .error {
            color: red;
            margin-left: 100px;
        }
        .success {
            color: green;
            margin-left: 100px;
        }
    </style>
</head>
<body>
    <h1>用户验证</h1>
    <form id="userForm">
        <div class="form-group">
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" required>
            <div class="error" id="usernameError"></div>
        </div>
        <div class="form-group">
            <label for="email">邮箱:</label>
            <input type="email" id="email" name="email" required>
            <div class="error" id="emailError"></div>
        </div>
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required>
            <div class="error" id="passwordError"></div>
        </div>
        <button type="submit">提交</button>
    </form>
    <div class="success" id="successMessage"></div>

    <script>
        document.getElementById('userForm').addEventListener('submit', function(event) {
            event.preventDefault(); // 防止表单默认提交

            // 获取输入值
            const username = document.getElementById('username').value.trim();
            const email = document.getElementById('email').value.trim();
            const password = document.getElementById('password').value;

            // 清除之前的错误信息
            document.getElementById('usernameError').textContent = '';
            document.getElementById('emailError').textContent = '';
            document.getElementById('passwordError').textContent = '';
            document.getElementById('successMessage').textContent = '';

            let isValid = true;

            // 验证用户名
            if (username.length < 3) {
                document.getElementById('usernameError').textContent = '用户名必须至少3个字符！';
                isValid = false;
            }

            // 验证邮箱
            const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
            if (!emailRegex.test(email)) {
                document.getElementById('emailError').textContent = '请输入有效的邮箱！';
                isValid = false;
            }

            // 验证密码
            if (password.length < 6) {
                document.getElementById('passwordError').textContent = '密码必须至少6个字符！';
                isValid = false;
            }

            // 如果所有验证通过
            if (isValid) {
                document.getElementById('successMessage').textContent = '验证成功！';
            }
        });
    </script>
</body>
</html>